home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / sql-bench / bench-count-distinct next >
Encoding:
Text File  |  2004-10-28  |  7.2 KB  |  260 lines

  1. #!/usr/bin/perl
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18. #
  19. # Test of selecting on keys that consist of many parts
  20. #
  21. ##################### Standard benchmark inits ##############################
  22.  
  23. use Cwd;
  24. use DBI;
  25. use Getopt::Long;
  26. use Benchmark;
  27.  
  28. $opt_loop_count=10000;
  29. $opt_medium_loop_count=200;
  30. $opt_small_loop_count=10;
  31. $opt_regions=6;
  32. $opt_groups=100;
  33.  
  34. $pwd = cwd(); $pwd = "." if ($pwd eq '');
  35. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  36.  
  37. $columns=min($limits->{'max_columns'},500,($limits->{'query_size'}-50)/24,
  38.          $limits->{'max_conditions'}/2-3);
  39.  
  40. if ($opt_small_test)
  41. {
  42.   $opt_loop_count/=10;
  43.   $opt_medium_loop_count/=10;
  44.   $opt_small_loop_count/=10;
  45.   $opt_groups/=10;
  46. }
  47.  
  48. print "Testing the speed of selecting on keys that consist of many parts\n";
  49. print "The test-table has $opt_loop_count rows and the test is done with $columns ranges.\n\n";
  50.  
  51. ####
  52. ####  Connect and start timeing
  53. ####
  54.  
  55. $dbh = $server->connect();
  56. $start_time=new Benchmark;
  57.  
  58. ####
  59. #### Create needed tables
  60. ####
  61.  
  62. goto select_test if ($opt_skip_create);
  63.  
  64. print "Creating table\n";
  65. $dbh->do("drop table bench1" . $server->{'drop_attr'});
  66.  
  67. do_many($dbh,$server->create("bench1",
  68.                  ["region char(1) NOT NULL",
  69.                   "idn integer(6) NOT NULL",
  70.                   "rev_idn integer(6) NOT NULL",
  71.                   "grp integer(6) NOT NULL"],
  72.                  ["primary key (region,idn)",
  73.                   "unique (region,rev_idn)",
  74.                   "unique (region,grp,idn)"]));
  75. if ($opt_lock_tables)
  76. {
  77.   do_query($dbh,"LOCK TABLES bench1 WRITE");
  78. }
  79.  
  80. if ($opt_fast && defined($server->{vacuum}))
  81. {
  82.   $server->vacuum(1,\$dbh);
  83. }
  84.  
  85. ####
  86. #### Insert $opt_loop_count records with
  87. #### region:    "A" -> "E"
  88. #### idn:     0 -> count
  89. #### rev_idn:    count -> 0,
  90. #### grp:    distributed values 0 - > count/100
  91. ####
  92.  
  93. print "Inserting $opt_loop_count rows\n";
  94.  
  95. $loop_time=new Benchmark;
  96. $query="insert into bench1 values (";
  97. $half_done=$opt_loop_count/2;
  98. for ($id=0,$rev_id=$opt_loop_count-1 ; $id < $opt_loop_count ; $id++,$rev_id--)
  99. {
  100.   $grp=$id*3 % $opt_groups;
  101.   $region=chr(65+$id%$opt_regions);
  102.   do_query($dbh,"$query'$region',$id,$rev_id,$grp)");
  103.   if ($id == $half_done)
  104.   {                # Test with different insert
  105.     $query="insert into bench1 (region,idn,rev_idn,grp) values (";
  106.   }
  107. }
  108.  
  109. $end_time=new Benchmark;
  110. print "Time to insert ($opt_loop_count): " .
  111.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  112.  
  113. if ($opt_lock_tables)
  114. {
  115.   do_query($dbh,"UNLOCK TABLES");
  116. }
  117.  
  118. if ($opt_fast && defined($server->{vacuum}))
  119. {
  120.   $server->vacuum(0,\$dbh,"bench1");
  121. }
  122.  
  123. if ($opt_lock_tables)
  124. {
  125.   do_query($dbh,"LOCK TABLES bench1 WRITE");
  126. }
  127.  
  128. ####
  129. #### Do some selects on the table
  130. ####
  131.  
  132. select_test:
  133.  
  134.  
  135.  
  136. if ($limits->{'group_distinct_functions'})
  137. {
  138.   print "Testing count(distinct) on the table\n";
  139.   $loop_time=new Benchmark;
  140.   $rows=$estimated=$count=0;
  141.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  142.   {
  143.     $count++;
  144.     $rows+=fetch_all_rows($dbh,"select count(distinct region) from bench1");
  145.     $end_time=new Benchmark;
  146.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  147.                        $opt_medium_loop_count));
  148.   }
  149.   print_time($estimated);
  150.   print " for count_distinct_key_prefix ($count:$rows): " .
  151.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  152.  
  153.   $loop_time=new Benchmark;
  154.   $rows=$estimated=$count=0;
  155.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  156.   {
  157.     $count++;
  158.     $rows+=fetch_all_rows($dbh,"select count(distinct grp) from bench1");
  159.     $end_time=new Benchmark;
  160.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  161.                        $opt_medium_loop_count));
  162.   }
  163.   print_time($estimated);
  164.   print " for count_distinct ($count:$rows): " .
  165.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  166.  
  167.   $loop_time=new Benchmark;
  168.   $rows=$estimated=$count=0;
  169.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  170.   {
  171.     $count++;
  172.     $rows+=fetch_all_rows($dbh,"select count(distinct grp),count(distinct rev_idn) from bench1");
  173.     $end_time=new Benchmark;
  174.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  175.                        $opt_medium_loop_count));
  176.   }
  177.   print_time($estimated);
  178.   print " for count_distinct_2 ($count:$rows): " .
  179.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  180.  
  181.   $loop_time=new Benchmark;
  182.   $rows=$estimated=$count=0;
  183.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  184.   {
  185.     $count++;
  186.     $rows+=fetch_all_rows($dbh,"select region,count(distinct idn) from bench1 group by region");
  187.     $end_time=new Benchmark;
  188.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  189.                        $opt_medium_loop_count));
  190.   }
  191.   print_time($estimated);
  192.   print " for count_distinct_group_on_key ($count:$rows): " .
  193.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  194.  
  195.   $loop_time=new Benchmark;
  196.   $rows=$estimated=$count=0;
  197.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  198.   {
  199.     $count++;
  200.     $rows+=fetch_all_rows($dbh,"select grp,count(distinct idn) from bench1 group by grp");
  201.     $end_time=new Benchmark;
  202.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  203.                        $opt_medium_loop_count));
  204.   }
  205.   print_time($estimated);
  206.   print " for count_distinct_group_on_key_parts ($count:$rows): " .
  207.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  208.  
  209.   $loop_time=new Benchmark;
  210.   $rows=$estimated=$count=0;
  211.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  212.   {
  213.     $count++;
  214.     $rows+=fetch_all_rows($dbh,"select grp,count(distinct rev_idn) from bench1 group by grp");
  215.     $end_time=new Benchmark;
  216.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  217.                        $opt_medium_loop_count));
  218.   }
  219.   print_time($estimated);
  220.   print " for count_distinct_group ($count:$rows): " .
  221.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  222.  
  223.   $loop_time=new Benchmark;
  224.   $rows=$estimated=$count=0;
  225.   $test_count=$opt_medium_loop_count/10;
  226.   for ($i=0 ; $i < $test_count ; $i++)
  227.   {
  228.     $count++;
  229.     $rows+=fetch_all_rows($dbh,"select idn,count(distinct region) from bench1 group by idn");
  230.     $end_time=new Benchmark;
  231.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  232.                        $test_count));
  233.   }
  234.   print_time($estimated);
  235.   print " for count_distinct_big ($count:$rows): " .
  236.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  237. }
  238.  
  239. ####
  240. #### End of benchmark
  241. ####
  242.  
  243. if ($opt_lock_tables)
  244. {
  245.   do_query($dbh,"UNLOCK TABLES");
  246. }
  247. if (!$opt_skip_delete)
  248. {
  249.   do_query($dbh,"drop table bench1" . $server->{'drop_attr'});
  250. }
  251.  
  252. if ($opt_fast && defined($server->{vacuum}))
  253. {
  254.   $server->vacuum(0,\$dbh);
  255. }
  256.  
  257. $dbh->disconnect;                # close connection
  258.  
  259. end_benchmark($start_time);
  260.